import { useRoutes, Navigate, RouteObject } from "react-router-dom";
import Login from "@/pages/login";
import User from "@/pages/user";
import Layout from "@/pages/layout";
import lazyload from "./lazyload";
import { lazy } from "react";

// 添加一个固定的延迟时间，以便你可以看到加载状态
function delayForDemo(promise: Promise<any>) {
    return new Promise(resolve => {
        setTimeout(resolve, 2000);
    }).then(() => promise);
}

export const router_item: Array<object> = [
    { path: "/", key: '/', label: "首页", hidden: true, element: <Navigate to="/layout/home" /> },
    // { path: "/login", key: 'login', label: "登录", hidden: true, element: <Login/>,meta:{ noAuth:true} },
    {
        path: "/layout",
        key:"layout",
        label: "控制台",
        element: <Layout />,
        children: [
            {
                path: "home",
                key:"home",
                label: "首页",
                element: lazyload(lazy(()=> delayForDemo(import("../pages/home"))))
            },
            {
                path: "login",
                key: "login",
                label: "登录页",
                element: <Login />
            },
            {
                path: "user",
                key: "user",
                label: "用户页",
                element: <User />
            },
            // {
            //     path: "edit",
            //     label: "用户页",
            //     element: <MarkdownEditor />
            // },
        ]
    },
    // { path: "/404", label: "404", hidden: true, element: <Error404/>,meta:{ noAuth:true} },
    // { path: "*", element: <Navigate to="404"/>,},
];

function GetRoutes() {
    const routes: RouteObject[] = useRoutes(router_item);
    return routes;
}

export default GetRoutes;
